package io.renren.modules.delay;

import cn.hutool.core.util.XmlUtil;
import com.google.common.collect.Lists;
import com.ruoyi.shadow.api.RemoteShadowResponse;
import com.ruoyi.shadow.api.service.RemoteShadowService;
import com.ruoyi.shadow.data.ShadowTsKvDto;
import io.netty.buffer.Unpooled;
import io.renren.modules.equipment.EquipmentEntity;
import io.renren.modules.equipment.EquipmentService;
import io.renren.modules.netty.EqmContextMap;
import io.renren.modules.netty.XmlService;
import io.renren.modules.service.IFeignClientCreateService;
import io.renren.util.CommonUtil;
import io.renren.util.HexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
@Slf4j
public class ReportTaskRunner {
  @Autowired
  private EquipmentService equipmentService;
  @Autowired
  private XmlService xmlService;
  @Autowired
  private IFeignClientCreateService feignClientCreateService;
  @Value("${ouganyun.shadow.url}")
  private String shadowUrl;

  /**
   *  <meter id="001" addr="0000000abc01" com="1" tp="202" name="测试电表 1">
   *  <function id="01" coding="01B00" error="192">113009.17</function>
   *  </meter>
   * @param taskBase
   */
  public void run(ReportTaskBase taskBase){
    log.info("begin report");
    List<EquipmentEntity> equipments = equipmentService.listByBuildingId(taskBase.getBuildingId());
    if (CollectionUtils.isEmpty(equipments)) {
      return;
    }
    Document report = xmlService.report(taskBase.getBuildingId());
    Node data = XmlUtil.getNodeByXPath("//root/data", report);
    for (int i = 0; i < equipments.size(); i++) {
      EquipmentEntity equipment = equipments.get(i);
      RemoteShadowService remoteShadowService = feignClientCreateService.newClientByUrl(RemoteShadowService.class, shadowUrl);
      RemoteShadowResponse<List<ShadowTsKvDto>> dianNengResponse = remoteShadowService.latestData(equipment.getTbDeviceId(), Lists.newArrayList("DianNeng"));
      if (CollectionUtils.isEmpty(dianNengResponse.getData())) {
        continue;
      }
      Element meter = report.createElement("meter");
      Element function = report.createElement("function");
      meter.setAttribute("id", CommonUtil.formatId(i + 1));
      meter.setAttribute("addr", equipment.getSn());
      meter.setAttribute("com", "1");
      meter.setAttribute("tp", "202");
      try {
        meter.setAttribute("name", new String(equipment.getName().getBytes("GB2312"),"GB2312"));
      } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
      }
      function.setAttribute("id", "01");
      function.setAttribute("coding", equipment.getCode());
      function.setAttribute("error","192");
      function.setTextContent(dianNengResponse.getData().get(0).getValue() + "");
      meter.appendChild(function);
      data.appendChild(meter);
    }
    String reportXml = XmlService.chageToMsg(report, null);
    EqmContextMap.getInstance().get(taskBase.getBuildingId()).writeAndFlush(Unpooled.copiedBuffer(HexUtils.hexStr2Bytes(reportXml)));
  }
}
